package com.sskj.mine.asset;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.Gravity;
import android.widget.TextView;

import com.alibaba.android.arouter.facade.annotation.Autowired;
import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.bulong.rudeness.RudenessScreenHelper;
import com.hey.lib.HeySpinner;
import com.sskj.common.adapter.slimadapter.IViewInjector;
import com.sskj.common.adapter.slimadapter.SlimAdapter;
import com.sskj.common.adapter.slimadapter.SlimInjector;
import com.sskj.common.base.App;
import com.sskj.common.box.decoration.DividerLineItemDecoration;
import com.sskj.common.util.DisposUtil;
import com.sskj.lib.RConfig;
import com.sskj.lib.base.BaseActivity;
import com.sskj.mine.R;
import com.sskj.mine.R2;
import com.sskj.mine.account.data.AccountBean;
import com.sskj.mine.asset.data.CoinTypeBean;
import com.sskj.mine.asset.data.RecordsBean;

import java.util.HashMap;
import java.util.List;

import butterknife.BindView;
import io.reactivex.Flowable;
import io.reactivex.disposables.Disposable;

/**
 * @author Hey
 * Create at  2019/05/04
 */
@Route(path = RConfig.MINE_ASSET_RECORDS)
public class AssetRecordsActivity extends BaseActivity<AssetRecordsPresenter> {

    @BindView(R2.id.records_list)
    RecyclerView recyclerView;
    @BindView(R2.id.right_tv)
    TextView rightTv;
    @BindView(R2.id.select_coin)
    HeySpinner selectCoin;

    private SlimAdapter recordsAdapter;
    private List<AccountBean> data;

    //  0 提币 1 充币
    @Autowired
    int type;
    private String pid;
    private Disposable typeDispo;

    @Override
    public int getLayoutId() {
        return R.layout.mine_activity_asset_records;
    }

    @Override
    public AssetRecordsPresenter getPresenter() {
        return new AssetRecordsPresenter();
    }

    @Override
    public void initView() {
        ARouter.getInstance().inject(this);
        if (type == 0) {
            setTitle(getString(R.string.mine_assetRecordsActivity1));
        } else {
            setTitle(getString(R.string.mine_assetRecordsActivity2));
        }
        HashMap<String, String> tibiMap = new HashMap<>();
        tibiMap.put("1", getString(R.string.mine_assetRecordsActivity3));
        tibiMap.put("2", getString(R.string.mine_assetRecordsActivity4));
        tibiMap.put("3", getString(R.string.mine_assetRecordsActivity5));
        tibiMap.put("4", getString(R.string.mine_assetRecordsActivity6));
        tibiMap.put("5", getString(R.string.mine_assetRecordsActivity7));
        HashMap<String, String> chongbiMap = new HashMap<>();
        chongbiMap.put("1", getString(R.string.mine_assetRecordsActivity8));
        chongbiMap.put("2", getString(R.string.mine_assetRecordsActivity9));
        selectCoin.setItemGravity(Gravity.CENTER);

        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        recyclerView.addItemDecoration(new DividerLineItemDecoration(this)
                .setFirstDraw(false)
                .setLastDraw(false)
                .setPaintWidth((int) RudenessScreenHelper.pt2px(App.INSTANCE,20))
                .setDividerColor(color(R.color.transparent)));
        recordsAdapter = SlimAdapter.create().register(R.layout.mine_item_asset_records, new SlimInjector<RecordsBean>() {
            @Override
            public void onInject(RecordsBean data, IViewInjector injector, List<Object> list) {

                injector
                        .text(R.id.item_address_title, type == 0 ? getString(R.string.mine_addressListActivity1) : getString(R.string.mine_assetRecordsActivity11))
                        .text(R.id.item_num_title, type == 0 ? getString(R.string.mine_assetRecordsActivity12) : getString(R.string.mine_assetRecordsActivity13))
                        .text(R.id.item_address, data.getAddress())
                        .text(R.id.item_num, data.getPrice())
                        .text(R.id.item_time, data.getAddtime())
                        .text(R.id.item_time2, data.getCheck_time())
                        .text(R.id.item_status, type == 0 ? tibiMap.get(data.getState()) : chongbiMap.get(data.getState()));
            }

        }).attachTo(recyclerView);
    }

    @Override
    public void initData() {
        mPresenter.getAssetType();
    }

    public static void start(Context context) {
        Intent intent = new Intent(context, AssetRecordsActivity.class);
        context.startActivity(intent);
    }

    public void updateType(List<CoinTypeBean> data) {
        if (data==null||data.size()<=0){
            return;
        }
        pid = data.get(0).getPid();
        refresh();
        typeDispo = Flowable.fromIterable(data)
                .map(coinTypeBean -> coinTypeBean.getPname())
                .toList()
                .subscribe((strings, throwable) -> {
                    selectCoin.attachData(strings);
                    selectCoin.setOnSelectListener(position -> {
                        pid = data.get(position).getPid();
                        refresh();
                    });

                });
    }

    private void refresh() {
        mPresenter.getAssetRecord(type == 1 ? "recharge" : "cash", pid);
    }

    @Override
    protected void onDestroy() {
        DisposUtil.close(typeDispo);
        super.onDestroy();
    }
}
